<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <title>生意帮</title>
    <link rel="stylesheet" type="text/css" href="css/api.css" />
    <link rel="stylesheet" type="text/css" href="css/common.css" />
    <link rel="stylesheet" type="text/css" href="css/aui.css" />
    <link rel="stylesheet" type="text/css" href="css/aui-pull-refresh.css" />
    <style>
        html,
        body {
            width: 100%;
            min-height: 100%;
            background: #ffffff;
        }

        img {
            width: 22px;
            margin: auto;
        }

        .aui-bar-tab-label {
            font-size: 0.5rem!important;
            padding-top: 4px;
            top: 1px;
        }

        .check_active {
            color: #ff0036;
        }
        /*遮罩层*/

        .cover {
            display: none;
            position: fixed;
            background-color: rgba(0, 0, 0, .2);
            width: 100vw;
            height: 2.5rem;
            bottom: 0;
            z-index: 10;
        }
    </style>
</head>

<body>
    <div id="app">
        <footer class="aui-bar aui-bar-tab" id="footer">
            <div class="aui-bar-tab-item aui-active " tapmode>

                <img src="image/footer/yuantou_on.png" alt="">
                <div class="aui-bar-tab-label check_active">源头产品</div>
            </div>
            <div class="aui-bar-tab-item" tapmode>
                <img src="image/footer/bendi_off.png" alt="">
                <div class="aui-bar-tab-label">本地服务</div>
            </div>
            <div class="aui-bar-tab-item" tapmode>
                <span class="aui-badge" id="cartBadge" style="display: none"></span>
                <img src="image/footer/gwc_off.png" alt="">
                <div class="aui-bar-tab-label">购物车</div>
            </div>
            <div class="aui-bar-tab-item" tapmode>
                <div class="aui-dot aui-hide"></div>
                <img src="image/footer/shejiao_off.png" alt="">
                <div class="aui-bar-tab-label">社交</div>
            </div>
            <div class="aui-bar-tab-item" tapmode>
                <img src="image/footer/wode_off.png" alt="">
                <div class="aui-bar-tab-label">我的</div>
            </div>

        </footer>
    </div>
</body>
<script type="text/javascript" src="script/api.js"></script>
<script type="text/javascript" src="script/zepto.js"></script>
<script type="text/javascript" src="script/vue.min.js"></script>
<script type="text/javascript" src="script/utils.js"></script>
<script type="text/javascript" src="script/common.js"></script>
<script type="text/javascript" src="script/aui-tab.js"></script>
<script type="text/javascript" src="script/aui-scroll.js"></script>
<script type="text/javascript" src="script/aui-pull-refresh.js"></script>
<script type="text/javascript">
    var footer = $api.byId('footer');
    var footerPos = $api.offset(footer);

    apiready = function() {

        api.addEventListener({
            name: 'offline'
        }, function(ret, err) {
            // alert('断网了');

            alert("当前网络已断开，app将无法正常使用，请及时检查网络！！！")
        });

        api.addEventListener({
            name: 'online'
        }, function(ret, err) {
            // alert('已连接到网络');

        });


        api.addEventListener({
            name: 'cartNum'
        }, function(ret, err) {
            if (ret.value.num == 0) {
                $('#cartBadge').hide()
            } else {
                $('#cartBadge').show()
                $('#cartBadge').text(ret.value.num)
            }

        });
        //setRefresh()
        openframeinstance('one_frame/one_index.html', 0, 0)

        api.addEventListener({
            name: 'close_four'
        }, function(ret, err) {
            if (ret) {
                var frm_index = ret.index - 1;
                var frm_name = ['yuantou', 'bendi', 'gwc', 'shejiao', 'wode'];
                if (ret.value.close && ret.value.index == 3) {
                    for (var i = 0; i < 5; i++) {
                        if (i == 0) {
                            $('.aui-bar-tab-item').eq(i).find('img').prop('src', 'image/footer/' + frm_name[i] + '_on.png')
                            $('.aui-bar-tab-item').eq(i).find('div').addClass('check_active')
                        } else {
                            $('.aui-bar-tab-item').eq(i).find('img').prop('src', 'image/footer/' + frm_name[i] + '_off.png')
                            $('.aui-bar-tab-item').eq(i).find('div').removeClass('check_active')
                        }
                    }
                    openframeinstance('one_frame/one_index.html', 0, 0)
                } else if (ret.value.close && ret.value.index == 4) {
                    for (var i = 0; i < 5; i++) {
                        if (i == 4) {
                            $('.aui-bar-tab-item').eq(i).find('img').prop('src', 'image/footer/' + frm_name[i] + '_on.png')
                            $('.aui-bar-tab-item').eq(i).find('div').addClass('check_active')
                        } else {
                            $('.aui-bar-tab-item').eq(i).find('img').prop('src', 'image/footer/' + frm_name[i] + '_off.png')
                            $('.aui-bar-tab-item').eq(i).find('div').removeClass('check_active')
                        }
                    }
                    openframeinstance('five_frame/index.html', 0, 5)
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
        //极光推送
        var ajpush = null;
        ajpush = api.require('ajpush');
        ajpush.getRegistrationId(function(ret) {
            $api.setStorage('ajpushId', ret.id);
        });
        var param = {
            alias: 'myalias',
            tags: ['tag1', 'tag2']
        };
        ajpush.bindAliasAndTags(param, function(ret) {
            var statusCode = ret.statusCode;
        });
        ajpush.init(function(ret) {
            //console.log(JSON.stringify(ret))
            if (ret && ret.status) {
                //success
                ajpush.setListener(function(ret) {
                    var id = ret.id;
                    var title = ret.title;
                    var content = ret.content;
                    var extra = ret.extra;
                    console.log("id=" + id + ",title=" + title + ",content=" + content + ",extra=" + extra);
                });
            }
        });
        //极光推送
        //推送
        // var uzgetuisdk = api.require('pushGeTui');
        // uzgetuisdk.initialize(function(ret) {
        //     var value = "";
        //     switch (ret.type) {
        //         case 'cid':
        //             value = 'cid:' + ret.cid;
        //             $api.setStorage('gt_cid', ret.cid)
        //             break;
        //         case 'payload':
        //             value = 'payload:' + ret.payload;
        //             break;
        //         case 'occurError':
        //             value = 'occurError';
        //             break;
        //         case 'onNotificationMessageClicked':
        //             value = 'onNotificationMessageClicked' + ret.onNotificationMessageClicked
        //             break;
        //         case 'onNotificationMessageArrived':
        //             value = 'onNotificationMessageArrived' + ret.onNotificationMessageArrived
        //             break;
        //     }
        //
        //     // console.log($api.getStorage('gt_cid'))
        //
        // });
        //推送

        // 初始化融云
        var rong = api.require('rongCloud2');
        rong.init(function(ret, err) {
            if (ret.status == 'success') {
                // console.log('初始化融云成功了')
            } else {
                api.toast({
                    msg: err.code
                });
            }
        });

        // 监听实时消息
        var msg_num = 0;
        var msg_num_ON = true;
        rong.setOnReceiveMessageListener(function(ret, err) {
            //console.log(JSON.stringify(ret))
            if (ret) {
                msg_num++;
                if (msg_num == 1) {
                    api.notification({
                        sound: 'default'
                    });
                }
                if (msg_num_ON) {
                    msg_num_ON = !msg_num_ON
                    setTimeout(function() {
                        msg_num = 0;
                        msg_num_ON = true;
                    }, 500)
                }

                api.sendEvent({
                    name: 'new_msg_event',
                    extra: {
                        dl_msg: ret.result.message,
                        ql_msg: ret.result.message,
                        msg_num: msg_num
                    }
                });
                add_tips_dot();
            }
        })

    };
    //刷新
    function setRefresh() {
        var pullRefresh = new auiPullToRefresh({
            container: document.querySelector('.aui-refresh-content'),
            triggerDistance: 100
        }, function(ret) {

            if (ret.status == "success") {
                setTimeout(function() {
                    pullRefresh.cancelLoading(); //刷新成功后调用此方法隐藏
                }, 1500)
            }
        })
    }


    var tab = new auiTab({
        element: document.getElementById("footer"),
        index: 1,
        repeatClick: true
    }, function(ret) {

        switch (ret.index) {
            case 1:
                openframeinstance('one_frame/one_index.html', 0, 1)
                break;
            case 2:
                $api.rmStorage('province')
                $api.rmStorage('city')
                $api.rmStorage('longitude')
                $api.rmStorage('latitude')
                $api.rmStorage('address')
                openframeinstance('two_frame/index.html', 0, 2)
                break;
            case 3:
                openframeinstance('three_frame/index.html', 0, 3)
                break;
            case 4:
                openframeinstance('four_frame/four_index.html', 0, 4)
                break;
            case 5:
                openframeinstance('five_frame/index.html', 0, 5)
                break;
            default:
                console.log('home')
                break;

        }

        var frm_index = ret.index - 1;
        var frm_name = ['yuantou', 'bendi', 'gwc', 'shejiao', 'wode'];

        for (var i = 0; i < 5; i++) {
            if (i == frm_index) {
                $('.aui-bar-tab-item').eq(i).find('img').prop('src', 'image/footer/' + frm_name[i] + '_on.png')
                $('.aui-bar-tab-item').eq(i).find('div').addClass('check_active')
            } else {
                $('.aui-bar-tab-item').eq(i).find('img').prop('src', 'image/footer/' + frm_name[i] + '_off.png')
                $('.aui-bar-tab-item').eq(i).find('div').removeClass('check_active')
            }
        }

    });


    // 打开frame
    function openframeinstance(frame, marginTop, module_index) {

        if (module_index != 4 && module_index != 3) {
            api.openFrame({
                reload: true,
                name: frame,
                url: './html/' + frame + '',
                rect: {
                    x: 0,
                    y: 0,
                    w: 'auto',
                    h: api.frameHeight - marginTop - (footerPos.h)
                },
                pageParam: {
                    name: '1'
                },
                // bounces: isBounce,
                bounces: false,
                vScrollBarEnabled: false,
                hScrollBarEnabled: false,
                delay: 200
            });
        } else if (module_index == 3) {
            all_open_login(function() {
                api.openFrame({
                    reload: true,
                    name: frame,
                    url: './html/' + frame + '',
                    rect: {
                        x: 0,
                        y: 0,
                        w: 'auto',
                        h: api.frameHeight - marginTop - (footerPos.h)
                    },
                    pageParam: {
                        name: '1'
                    },
                    // bounces: isBounce,
                    bounces: false,
                    vScrollBarEnabled: false,
                    hScrollBarEnabled: false,
                    delay: 200
                });
            }, 'index')
        } else {
            remove_tips_dot();
            all_open_login(function() {
                api.openWin({
                    name: frame,
                    url: './html/' + frame + '',
                    pageParam: {
                        name: 'test'
                    }
                });
            }, 'index')
        }

    }


    // 有未读消息
    function add_tips_dot() {
        $('.aui-dot').removeClass('aui-hide');
    }
    // 无未读消息
    function remove_tips_dot() {
        $('.aui-dot').addClass('aui-hide');
    }
</script>

</html>
